<?php

namespace CSan\NutrientBundle\DataFixtures\ORM;

use Doctrine\Common\Persistence\ObjectManager,
    Doctrine\Common\DataFixtures\FixtureInterface;

use Symfony\Component\DependencyInjection\ContainerAwareInterface,
    Symfony\Component\DependencyInjection\ContainerInterface;

use CSan\UtilBundle\Lib\FileSystem\CsvFile;

use CSan\NutrientBundle\Entity\Food;

class FoodLoader implements FixtureInterface, ContainerAwareInterface
{
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function load(ObjectManager $manager)
    {
        $csvFile = new CsvFile(__DIR__ . '/FoodData.csv', ';', '"');
        $rows = $csvFile->getData();
        
        // Delete header row
        array_shift($rows);

        foreach ($rows as $row) {
            $publicId = $this->container->get('csan.util.number')->uuid();

            $food = new Food($publicId);

            $name = $row[1];
            $food->setName($name);

            // Energie (kcal)
            $energy = $this->parseFloat($this->parseException($row[13]));
            $food->setEnergy($energy);

            // Proteines
            $protein = $this->parseFloat($this->parseException($row[27]));
            $food->setProtein($protein);

            // Glucides
            $carbohydrates = $this->parseFloat($this->parseException($row[19]));
            $food->setCarbohydrates($carbohydrates);

            // Amidon
            $starch = $this->parseFloat($this->parseException($row[7]));
            $food->setStarch($starch);

            // Sucres
            $sugar = $this->parseFloat($this->parseException($row[32]));
            $food->setSugar($sugar);

            // Lipides
            $fat = $this->parseFloat($this->parseException($row[21]));
            $food->setFat($fat);

            // Acides gras saturés
            $fattyAcidsSaturated = $this->parseFloat($this->parseException($row[5]));
            $food->setFattyAcidsSaturated($fattyAcidsSaturated);

            // Acides gras monoinsaturés
            $fattyAcidsMonounsaturated = $this->parseFloat($this->parseException($row[3]));
            $food->setFattyAcidsMonounsaturated($fattyAcidsMonounsaturated);

            // Acides gras polyinsaturés
            $fattyAcidsPolyunsaturated = $this->parseFloat($this->parseException($row[4]));
            $food->setFattyAcidsPolyunsaturated($fattyAcidsPolyunsaturated);

            // Eau
            $water = $this->parseFloat($this->parseException($row[12]));
            $food->setWater($water);

            // Alcool
            $alcohol = $this->parseFloat($this->parseException($row[6]));
            $food->setAlcohol($alcohol);

            // Fibres
            $fiber = $this->parseFloat($this->parseException($row[18]));
            $food->setFiber($fiber);

            // Calcium
            $calcium = $this->parseFloat($this->parseException($row[9]));
            $food->setCalcium($calcium);

            // Cuivre
            $copper = $this->parseFloat($this->parseException($row[11]));
            $food->setCopper($copper);

            // Iode
            $iodine = $this->parseFloat($this->parseException($row[20]));
            $food->setIodine($iodine);

            // Fer
            $iron = $this->parseFloat($this->parseException($row[17]));
            $food->setIron($iron);

            // Magnésium
            $magnesium = $this->parseFloat($this->parseException($row[22]));
            $food->setMagnesium($magnesium);

            // Manganèse
            $manganese = $this->parseFloat($this->parseException($row[23]));
            $food->setManganese($manganese);

            // Phosphore
            $phosphorus = $this->parseFloat($this->parseException($row[24]));
            $food->setPhosphorus($phosphorus);

            // Potassium
            $potassium = $this->parseFloat($this->parseException($row[26]));
            $food->setPotassium($potassium);
            
            // Sélénium
            $selenium = $this->parseFloat($this->parseException($row[30]));
            $food->setSelenium($selenium);

            // Sodium
            $sodium = $this->parseFloat($this->parseException($row[31]));
            $food->setSodium($sodium);

            // Zinc
            $zinc = $this->parseFloat($this->parseException($row[43]));
            $food->setZinc($zinc);

            // Vitamine A (Rétinol + Bêta-carotène)
            $vitaminA = $this->parseFloat($this->parseException($row[29])) + $this->parseFloat($this->parseException($row[8]));
            $food->setVitaminA($vitaminA);

            // Vitamine B1 (Thiamine)
            $vitaminB1 = $this->parseFloat($this->parseException($row[33]));
            $food->setVitaminB1($vitaminB1);
            
            // Vitamine B2 (Riboflavine)
            $vitaminB2 = $this->parseFloat($this->parseException($row[35]));
            $food->setVitaminB2($vitaminB2);

            // Vitamine B3 (PP ou Niacine)
            $vitaminB3 = $this->parseFloat($this->parseException($row[36]));
            $food->setVitaminB3($vitaminB3);

            // Vitamine B5 (Acide pantothénique)
            $vitaminB5 = $this->parseFloat($this->parseException($row[37]));
            $food->setVitaminB5($vitaminB5);

            // Vitamine B6 (Pyridoxine)
            $vitaminB6 = $this->parseFloat($this->parseException($row[38]));
            $food->setVitaminB6($vitaminB6);

            // Vitamine B9 (Folates totaux)
            $vitaminB9 = $this->parseFloat($this->parseException($row[39]));
            $food->setVitaminB9($vitaminB9);

            // Vitamine B12 (Cobalamines)
            $vitaminB12 = $this->parseFloat($this->parseException($row[34]));
            $food->setVitaminB12($vitaminB12);
            
            // Vitamine C
            $vitaminC = $this->parseFloat($this->parseException($row[40]));
            $food->setVitaminC($vitaminC);

            // Vitamine D
            $vitaminD = $this->parseFloat($this->parseException($row[41]));
            $food->setVitaminD($vitaminD);

            // Vitamine E
            $vitaminE = $this->parseFloat($this->parseException($row[42]));
            $food->setVitaminE($vitaminE);
            
            $manager->persist($food);
        }

        $manager->flush();
    }

    public function parseException($value)
    {
        $value = strtolower(trim($value));

        if ($value == 'tr' OR $value == 'traces') {
            return 0.0001;
        }

        if ($value == '-' OR $value == '' OR $value == 'unknown') {
            return 0;
        }

        if (substr($value, 0, 1) == '<') {
            return $this->parseFloat(trim(substr($value, 1))) / 2;
        }
        
        return $value;
    }
    
    public function parseInt($value)
    {
        return intval($value);
    }

    public function parseFloat($value)
    {
        if (is_string($value)) {
            $value = str_replace(',', '.', $value);
        }
        return floatval($value);
    }
}